package com.braveman.im.ui.activity.message;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.GetDetailsByImGroupId;
import com.braveman.im.bean.GroupDetailsBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.ComplainActivity;
import com.braveman.im.ui.activity.InviteFriendsJoinActivity;
import com.braveman.im.ui.weiget.ButtonItem;
import com.braveman.im.ui.weiget.CustomPopwindow;
import com.braveman.im.ui.weiget.DialogUtils;
import com.braveman.im.ui.weiget.MenuPopup;
import com.braveman.im.ui.weiget.QrCodePopup;
import com.braveman.im.ui.weiget.UISwitchButton;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jaeger.library.StatusBarUtil;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class GroupInfoActivity extends BaseActivity {
    private ButtonItem mBtiActivityGroupName;
    private ButtonItem mBtiActivityGroupQrCode;
    private ButtonItem mBtiActivityGroupHeadNotify;
    private ButtonItem mBtiActivityGroupMember;
    private ButtonItem mBtiActivityGroupAddFriends;
    private ButtonItem mBtiActivityGroupMessageSetting;
    private UISwitchButton mSwBtnActivityGroupMessageCanHint;
    private ButtonItem mBtiActivityGroupMessageClear;
    private ButtonItem mBtiActivityGroupComplain;
    private ButtonItem mBtiActivityGroupStarLevel;
    private ButtonItem mBtiActivityGroupTopSetting;
    private ButtonItem mBtiActivityGroupManage;
    private TextView mBtiActivityGroupManageLine;


    private String user_id;
    private QrCodePopup qrCodePopup;
    private MenuPopup mMenuPopup;
    private String groupId;
    private String role;//身份


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("群资料").setLeftBtnIcon(R.mipmap.back_black)
                .setOnLeftBtnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ActivityUtils.finishActivity(GroupInfoActivity.this);
                    }
                });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_group_info;
    }

    @Override
    protected void initIntentData(Intent callIntent) {

        user_id = callIntent.getStringExtra("user_id");
        groupId = callIntent.getStringExtra("groupId");
    }

    @Override
    protected void initChildView() {
        mBtiActivityGroupName = (ButtonItem) findViewById(R.id.bti_activity_group_name);
        mBtiActivityGroupQrCode = (ButtonItem) findViewById(R.id.bti_activity_group_qr_code);
        mBtiActivityGroupHeadNotify = (ButtonItem) findViewById(R.id.bti_activity_group_head_notify);
        mBtiActivityGroupMember = (ButtonItem) findViewById(R.id.bti_activity_group_member);
        mBtiActivityGroupAddFriends = (ButtonItem) findViewById(R.id.bti_activity_group_add_friends);
        mBtiActivityGroupMessageSetting = (ButtonItem) findViewById(R.id.bti_activity_group_message_setting);
        mSwBtnActivityGroupMessageCanHint = (UISwitchButton) findViewById(R.id.sw_btn_activity_group_message_can_hint);
        mBtiActivityGroupMessageClear = (ButtonItem) findViewById(R.id.bti_activity_group_message_clear);
        mBtiActivityGroupComplain = (ButtonItem) findViewById(R.id.bti_activity_group_complain);
        mBtiActivityGroupStarLevel = (ButtonItem) findViewById(R.id.bti_activity_group_star_level);
        mBtiActivityGroupTopSetting = (ButtonItem) findViewById(R.id.bti_activity_group_manage);


        mBtiActivityGroupManage = (ButtonItem) findViewById(R.id.bti_activity_group_manage);
        mBtiActivityGroupManageLine = (TextView) findViewById(R.id.bti_activity_group_manage_line);

    }

    @Override
    protected void initChildData() {
        if (TextUtils.isEmpty(groupId)) {
            ArrayList<String> list = new ArrayList<>();
            list.add(user_id);
            ApiManager.getApi().getByImGroupIds(list).enqueue(new NetCallBack<List<GetDetailsByImGroupId>>() {
                @Override
                protected void onSuccess(Call<List<GetDetailsByImGroupId>> call, Response<List<GetDetailsByImGroupId>> response) {
                    if (response != null) {
                        if (response.body() != null && response.body().size() == 1) {
                            groupId = response.body().get(0).id;
                            doNetWork();
                        }

                    }
                }
            });
            return;
        }

        doNetWork();

    }

    private void doNetWork() {
        ApiManager.getApi().getGroupDetils(groupId).enqueue(new NetCallBack<GroupDetailsBean>() {
            @Override
            protected void onSuccess(Call<GroupDetailsBean> call, Response<GroupDetailsBean> response) {
                if (response != null) {
                    GroupDetailsBean groupDetailsBean = response.body();
                    mBtiActivityGroupName.tv_des.setText(groupDetailsBean.name);
                    role = groupDetailsBean.groupRole;
                    mBtiActivityGroupManage.setVisibility("member".equals(role) ? View.GONE : View.VISIBLE);
                    mBtiActivityGroupManageLine.setVisibility("member".equals(role) ? View.GONE : View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        mBtiActivityGroupName.setOnClickListener(this);
        mBtiActivityGroupQrCode.setOnClickListener(this);
        mBtiActivityGroupHeadNotify.setOnClickListener(this);
        mBtiActivityGroupMember.setOnClickListener(this);
        mBtiActivityGroupAddFriends.setOnClickListener(this);
        mBtiActivityGroupMessageSetting.setOnClickListener(this);
        mBtiActivityGroupMessageClear.setOnClickListener(this);
        mBtiActivityGroupComplain.setOnClickListener(this);
        mBtiActivityGroupStarLevel.setOnClickListener(this);
        mBtiActivityGroupTopSetting.setOnClickListener(this);
        mBtiActivityGroupManage.setOnClickListener(this);
        findViewById(R.id.bti_activity_group_top_setting).setOnClickListener(this);
        mSwBtnActivityGroupMessageCanHint.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                UIUtils.showToast(b ? "接收群消息开启" : "接收群消息关闭");
            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bti_activity_group_message_setting:
                DialogUtils.createPopwindow(this, "选择群的消息提醒方式", "取消", new CustomPopwindow.OnBtnClickListener("接受消息但不提醒") {
                    @Override
                    public void onClick(CustomPopwindow popwindow, View view) {
                        mBtiActivityGroupMessageSetting.tv_title_del.setText("接受消息但不提醒");
                    }
                }, new CustomPopwindow.OnBtnClickListener("屏蔽群消息") {
                    @Override
                    public void onClick(CustomPopwindow popwindow, View view) {
                        mBtiActivityGroupMessageSetting.tv_title_del.setText("屏蔽群消息");
                    }
                }).showAtLocation(mBtiActivityGroupMessageSetting, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 130);
                break;

            case R.id.bti_activity_group_member:
                Bundle bundle = new Bundle();
                bundle.putString("groupId", groupId);
                ActivityUtils.startActivity(GroupInfoActivity.this, GroupMemberActivity.class, bundle);
                break;
            case R.id.bti_activity_group_qr_code://群二维码
                Bundle bundle1 = new Bundle();
                bundle1.putString("qrCodeType", user_id);
                ActivityUtils.startActivity(GroupInfoActivity.this, QrcodeForMineActivity.class, bundle1);
//                qrCodePopup = new QrCodePopup(GroupInfoActivity.this,user_id);
//                qrCodePopup.showPopupWindow(v);

//                mMenuPopup = new MenuPopup(GroupInfoActivity.this);
//                mMenuPopup.showPopupWindow(v);

                break;
            case R.id.bti_activity_group_top_setting://设置置顶
                Bundle bundle2 = new Bundle();
                bundle2.putString("groupId", groupId);
                ActivityUtils.startActivity(GroupInfoActivity.this, SettingGroupPostionActivity.class, bundle2);
                break;
            case R.id.bti_activity_group_head_notify:
                Bundle bundle4 = new Bundle();
                bundle4.putString("groupId", groupId);
                ActivityUtils.startActivity(GroupInfoActivity.this, AnnouncementActivity.class, bundle4);//群公告
                break;
            case R.id.bti_activity_group_complain:
                ActivityUtils.startActivity(GroupInfoActivity.this, ComplainActivity.class);//投诉
                break;
            case R.id.bti_activity_group_add_friends:
                Bundle bundle3 = new Bundle();
                bundle3.putInt("listType", 3);//1群 2 单位 3好友
                ActivityUtils.startActivity(GroupInfoActivity.this, InviteFriendsJoinActivity.class, bundle3);//我的好友
                break;
            case R.id.bti_activity_group_manage://管理群
                Bundle bundle5 = new Bundle();
                bundle5.putString("group", groupId);
                ActivityUtils.startActivity(GroupInfoActivity.this, GroupManagerActivity.class, bundle5);
                break;
        }
    }

}
